//
//  RootTabBarViewController.swift
//  Gaoloumi
//
//  Created by Jasper on 3/5/16.
//  Copyright © 2016 Jasper. All rights reserved.
//

import UIKit
//import SnapKit

class RootTabBarViewController: UITabBarController {
    
//    var tabCustomView: TabBarCutomView?

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.red
        
//        let mainCtl = MainViewController()
//        let mainNav = RootNavigationViewController(rootViewController:mainCtl)
//        
//        let circleCtl = CircleViewController()
//        let circleNav = RootNavigationViewController(rootViewController:circleCtl)
//        
//        let pictureCtl = PictureViewController()
//        let pictureNav = RootNavigationViewController(rootViewController:pictureCtl)
//        
//        let mineCtl = MineViewController()
//        let mineNav = RootNavigationViewController(rootViewController:mineCtl)
//        
//        
//        self.viewControllers = [mainNav,circleNav, pictureNav, mineNav]
        
//        self.tabBar.hidden = true
//        
//        tabCustomView = TabBarCutomView.init(frame:CGRectZero)
//        tabCustomView!.tabBarViewController = self
//        self.view.addSubview(tabCustomView!)
//        
//        let width = CGRectGetWidth(UIScreen.mainScreen().bounds)
//        let height = CGRectGetHeight(UIScreen.mainScreen().bounds)
//        
//        tabCustomView!.snp_makeConstraints{ (make) -> Void in
//            make.size.equalTo(CGSize(width: width, height: 44.0))
//            make.centerX.equalTo(view)
//            make.centerY.equalTo(height/2-22)
//            //            make.right.left.top.equalTo(view)
//            //            make.width.equalTo(44)
//        }
        
    }
}

class TabBarCutomView: UIView {
    
    fileprivate enum buttonTag: Int {
        case mainButtonTag      = 0
        case circleButtonTag    = 1
        case pictureButtonTag   = 2
        case mineButtonTag      = 3
    }
    
    fileprivate var mainButton: UIButton?
    fileprivate var circleButton: UIButton?
    fileprivate var pictureButton: UIButton?
    fileprivate var mineButton: UIButton?
    
    internal var tabBarViewController: RootTabBarViewController?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        initViews()
        self.backgroundColor = UIColor.white
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    fileprivate func initViews() {
        //        mainButton = createButton(normalImg:"", selectImg: "", tag: .mainButtonTag);
        //        circleButton = createButton(normalImg:"", selectImg: "", tag: .circleButtonTag);
        //        pictureButton = createButton(normalImg:"", selectImg: "", tag: .pictureButtonTag);
        //        mineButton = createButton(normalImg:"", selectImg: "", tag: .mineButtonTag);
        
        mainButton = createButton(normalImg:"tabbar1", selectImg:"tabbar1-dwon", tag: 0);
        circleButton = createButton(normalImg:"tabbar2", selectImg:"tabbar2-dwon", tag: 1);
        pictureButton = createButton(normalImg:"tabbar3", selectImg:"tabbar3-dwon", tag: 2);
        mineButton = createButton(normalImg:"tabbar4", selectImg:"tabbar4-dwon", tag: 3);
        mainButton?.isSelected = true // 默认选中第一个
        
        let view = UIView.init(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 0.5))
        view.backgroundColor = UIColor(hex: 0x999999)!
        self.addSubview(view)
    }
    
    fileprivate func createButton(normalImg:String, selectImg:String, tag:Int) -> UIButton {
        let button = UIButton(type: .custom)
        button.setImage(UIImage(named: normalImg), for: UIControlState())
        button.setImage(UIImage(named: selectImg), for: .selected)
        button.setImage(UIImage(named: selectImg), for: .highlighted)
        button.tag = tag
        button.addTarget(self, action: #selector(TabBarCutomView.buttonClicked(_:)), for: .touchUpInside)
        self.addSubview(button)
//        let width = Float(CGRectGetWidth(UIScreen.mainScreen().bounds))
//        let smallx = width/4
//        button.snp_makeConstraints{ (make) -> Void in
//            make.size.equalTo(CGSize(width: 64, height: 49))
//            make.centerX.equalTo(self.snp_left).offset(smallx/2+smallx*Float(tag))
//            make.centerY.equalTo(0)
//        }
        return button
    }
    
    func buttonClicked(_ sender:UIButton) {
        mainButton?.isSelected = false
        circleButton?.isSelected = false
        pictureButton?.isSelected = false
        mineButton?.isSelected = false
        sender.isSelected = true
        
        tabBarViewController?.selectedIndex = sender.tag;
    }
}

